<template>
  <view>
    <!-- 最外层的容器 -->
    <view class="my-settle-container">
      <!-- 全选区域 -->
      <label class="radio" @click="changeAllState">
        <radio color="#c00000" :checked="isFullCheck">
          <text>全选</text>
        </radio>
      </label>
      <!-- 合计区域 -->
      <view class="amount-box">
        合计：<text>￥{{checkedGoodsAmount}}</text>
      </view>
      <!-- 结算按钮 -->
      <view class="but-settle" @click="settlement">
        结算({{checkedCount}})
      </view>
    </view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex'
  export default {
    name:"my-settle",
    computed:{
      ...mapGetters('m_cart',['checkedCount','total','checkedGoodsAmount']),
      ...mapGetters('m_user',['addstr']),
      ...mapState('m_user',['token']),
      // 是否全选
      isFullCheck(){
        return this.total === this.checkedCount
      }
    },
    data() {
      return {
        // 倒计时的秒数
        seconds:3,
        // 定时器的 ID
        timer:null
      };
    },
    methods:{
      ...mapMutations('m_cart',['updateGoodsState']),
      ...mapMutations('m_user',['updateRedirectInfo']),
      changeAllState(){
        this.updateGoodsState(!this.isFullCheck)
      },
      // 点击结算的处理函数
      settlement(){
        if(!this.checkedCount) return uni.$showMsg('请选择要结算的商品');
        if(!this.addstr) return uni.$showMsg('请选择收货地址');
        // if(!this.token) return uni.$showMsg('请先登录');
        if(!this.token) return this.delayNavigate()
        // 实现微信支付功能
        this.payOrder()
      },
      delayNavigate(){
        this.seconds = 3
        this.showTips(this.seconds)
       this.timer =  setInterval(()=>{
          this.seconds --
          if(this.seconds <= 0){
            // 清除定时器
            clearInterval(this.timer)
            
            uni.switchTab({
              url:'/pages//my/my',
              success:()=>{
                this.updateRedirectInfo({
                  openType:'switchTab',
                  from:'/pages/cart/cart'
                })
              }
            })
            return;
          }
          this.showTips(this.seconds)
        },1000)
      },
      // 微信支付
      async payOrder(){
        // 1 创建订单
        const orderInfo = {
          // 开发期间，注释掉真实的订单价格
          // order_price:this.checkedGoodsAmount,
          // 写死订单总价为 1 分钱
          order_price:0.01,
          consignee_addr:this.addstr,
          goods:this.cart.filter(item => item.goods_state).map(item =>({
            goods_id:item.goods_id,
            goods_number:item.goods_count,
            goods_price:item.goods_price
            }))
        }
        // 发起请求创建订单
        const {data:response} = await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        if(response.meta.status !== 200) return uni.$showMsg('创建订单失败！')
        // 得到服务器响应的 “订单编号”
        const orderNumber = response.message.order_number;
        // 2 订单预支付
        const {data:response2} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder',{order_number:orderNumber})
        if(response2.meta.status !== 200) return uni.$showErrow('预付订单生成失败！')
        const payInfo = response2.message.pay;
        // 3 发起微信支付
        const [err,succ] = await uni.requsetPayment(payInfo)
        if(err) return uni.$$showMsg('订单未支付！')
        const {data:response3} = await uni.$http.post('/api/public/v1/my/orders/chkOrder',{order_number:orderNumber})
        if(response3.meta.status !== 200) return uni.$showMsg('订单未支付！')
        uni.showToast({
          title:'支付完成！',
          icon:'success'
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.my-settle-container{
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: white;
  padding-left: 5px;
  font-size: 14px;
  
  .radio{
    display: flex;
    align-items: center;
  }
  
  .amount{
    color: #c00000;
  }
  
  .but-settle{
    height: 50px;
    min-width: 100px;
    background-color: #c00000;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>